<?php
/**
 * The Mysql Improved driver extends the Core_Library_Database to provide
 * interaction with Mysql database
 * @author astolarski
 *
 */
class Core_Driver_Mysqlimproved extends Core_Library_Database {
	
	/**
	 * Connection holds MySqli resource
	 * @var $connection
	 */
	private $connection;
	/**
	 * Query to perform
	 * @var $query
	 */
	private $query;
	/**
	 * Results holds data retrieved from server
	 * 
	 * @var results
	 */
	private $result;
	
	/**
	 * create new connection to database
	 * @see Core_Library_Database::connect()
	 */
	public function connect(){
		/**
		 * connection parametrs
		 */
		$host = DATABASE_HOST;
		$user = DATABASE_USER;
		$password = DATABASE_PASSWORD;
		$database = DATABASE_NAME;
		/**
		 * this implementation required there ...
		 */
		$port = NULL;
		$socket = NULL;
		
		/**
		 * create new mysqli connection
		 */
		if($this->connection = new mysqli($host, $user, $password, $database, $port, $socket)){
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * break conection to database
	 * @see Core_Library_Database::disconnect()
	 */
	public function disconnect(){
		/**
		 * clean up connection
		 */
		if($this->connection->close()){
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Prepare query to execute
	 * @param $query
	 * @see Core_Library_Database::prepare()
	 */
	public function prepare($query){
		if(($query != NULL) && (trim($query) != '')){
			$this->query = $query;
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Execute a prepare query
	 * @see Core_Library_Database::query()
	 */
	public function query(){
		/**
		 * execute prepared query and store in result value
		 */
		if(isset($this->query)){
			$this->result = $this->connection->query($this->query);
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Fetch a row from the query result
	 * @param $type
	 * @return $row or FALSE
	 * @see Core_Library_Database::fetch()
	 */
	public function fetch($type = 'object'){
		if(isset($this->result)){
			switch ($type){
				case 'array':
					// fetch a row as array
					$row = $this->result->fetch_array();
					break;
				case 'object':
					// fecth a row as object
					$row = $this->result->fetch_object();
					break;
				default:
					$row = $this->result->fetch_object();
					break;
			}
			return $row;
		}
		return FALSE;
	}
	
	/**
	 * insert, udpate and delete functionality
	 * @return affected rows
	 * @see Core_Library_Database::data_operations()
	 */
	public function data_operations(){
		if(isset($this->result)){
			return $this->connection->affected_rows;
		}
		return FALSE;
	}
	
	/**
	 * Santize data to be used in a query
	 * @param array $variables
	 * @return santize data
	 * @see Core_Library_Database::escape()
	 */
	public function escape(array $variables = null){
		foreach($variables as $key => $value){
			if(is_array($key)){
				$variables[$key] = $this->escape($value);
			}
			else {
				if(get_magic_quotes_gpc()){
					$variables[$key] = $this->connection->real_escape_string(stripslashes($value));
				}
				else {
					$variables[$key] = $this->connection->real_escape_string($value);
				}
			}
		}
		return $variables;
	}
}